import requests
from coin.experimental.ziyan.interval_feed_tool.read_coingekco_interval import get_coingecko_df
import datetime
import os
import json
import toml
# use coin/python/pyrunner to launch
fast_feed_path = "/remote/iosg/coin-2/feed_cache2/feed-05.cn-hongkong.aliyun/%s/Spot.Coingecko.v3.non_tbs/"
pi_path = "../../coin_product_info/data/%s/Spot.Coingecko.v3.json"

class SymbologyInfo(object):
  def __init__(self):
    with open(f'../data/coin2/symbology/translation.toml') as f:
      self.translation_per_mea = toml.load(f)
    with open(f'../data/coin2/symbology/universe.toml') as f:
      self.universe_per_me = toml.load(f)

  def currency_list(self, me):
    return self.universe_per_me[me]['currencies']

  def product_list(self, me):
    return self.universe_per_me[me]['products']

  def norm_to_native_currency(self, mea, c):
    t = self.translation_per_mea[mea]
    if c in t['currencies']:
      c = t['currencies'][c]

    if t['native_currency_lower_case']:
      c = c.lower()
    return c

  def native_to_norm_currency(self, mea, c):
    t = self.translation_per_mea[mea]
    c = c.upper()
    if 'currencies' in t:
      for (k, v) in t['currencies'].items():
        if c == v:
          c = k
          break
    return c



def get_pi_from_exchange():
    # url = "https://api.coingecko.com/api/v3/coins/list"
    # data=requests.get(url).json()
    path = "/home/ziyan/tmp/coingecko_pi.json"
    with open(path, "r") as fp:
        data =json.load(fp)
    symbol_id = {x["symbol"].upper()+"-USD":x["id"] for x in data}
    symbol_id['LN-USD'] = 'link'
    symbol_id['COCOS-USD'] = 'cocos-bcx'
    symbol_id['ITAMCUBE-USD'] ='cube'
    return symbol_id
symbol_id = get_pi_from_exchange()

def get_fastfeed_symbol(date):
    date_str = date.strftime('%Y%m%d')
    path = fast_feed_path % date_str
    for r, d, f in os.walk(path):
        files = f
    ignore = ["BCH-USD.W0.lz4","BSV-USD.W0.lz4","LUNA-USD.W0.lz4","XNO-USD.W0.lz4"]
    symbols = [x.split('-')[0]+'-USD' for x in files if x not in ignore]
    return set(symbols)

def get_pi_symbol(date):
    date_str = date.strftime('%Y%m%d')
    file = pi_path % date_str
    if not os.path.exists(file):
       return 
    with open(file, 'r') as fp:
        json_obj = json.load(fp)
    pis=json_obj['product_infos']
    symbol = [x['symbol'] for x in pis]
    return set(symbol)

def get_interval_feed_symbol(date):
    date_str = date.strftime('%Y%m%d')
    df = get_coingecko_df(date_str)
    # ignore = {"BCH-USD","BSV-USD","LUNA-USD","XNO-USD"}
    symbols = [x[0] for x in df.columns]
    return set(symbols) #- ignore


si = SymbologyInfo()
def add_pi(date,need_add):
    add_pis=[]
    for symbol in need_add:
        if symbol in symbol_id.keys():
            native_symbol = symbol_id[symbol]
        else:
            native_symbol = symbol[:-4]
        native_base = native_symbol
        native_quote = 'USD'
        base = symbol[:-4]
        base = si.native_to_norm_currency('Spot.Coingecko.v3', base)
        quote = 'USD'
        symbol = f"%s-%s" % (base, quote)

        if len(symbol.split("-")) > 2:
            print("{}, filter symbol: {}".format('Spot.Coingecko.v3', symbol))
            continue
        price_ticksize =[{"from": 0,"to": 1000000000.0,"value": 0}]
        qty_ticksize = [{"from": 0,"to": 1000000000.0,"value": 0}]
        pi = dict(
            symbol=symbol,
            base=base,
            quote=quote,
            native_symbol=native_symbol,
            native_base=native_base,
            native_quote=native_quote,
            price_ticksize=price_ticksize,
            qty_ticksize=qty_ticksize,
            maker_fee=0,
            taker_fee=0,
            fetched_time=1689213814086854181)
        add_pis.append(pi)
    date_str = date.strftime('%Y%m%d')
    file = pi_path % date_str
    with open(file, 'r') as fp:
        json_obj = json.load(fp)
    json_obj['product_infos'].extend(add_pis)
    with open(file, 'w') as fp:
       json.dump(json_obj,fp,indent=2)


start = datetime.date(2022,8,11)
end = datetime.date(2023,7,30)
symbol_id = get_pi_from_exchange()
missing_pi=set()
ignore = ["BCH-USD","BSV-USD","LUNA-USD","XNO-USD"]
while start < end:
    pis=get_pi_symbol(start)
    if not pis :
       start += datetime.timedelta(days=1)
       continue
    ffs=get_fastfeed_symbol(start)
    ifs=get_interval_feed_symbol(start)
    need_add1 = ffs - pis
    need_add2 = ifs - pis
    need_add = need_add1 | need_add2
    need_add = {x for x in need_add if x not in ignore}
    issue = ifs - ffs
    print(start)
    for s in need_add:
       if s not in symbol_id.keys():
          missing_pi.add(s)
    if need_add:
      print(f"add {str(len(need_add))} symbols")
      # add_pi(start,need_add)
    print("-"*30)
    start += datetime.timedelta(days=1)
print(missing_pi)
